package com.mlan.userap.untils;

import android.Manifest;
import android.content.pm.PackageManager;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PermissionHelper {
    // 单例实例
    private static final PermissionHelper INSTANCE = new PermissionHelper();

    // 回调接口，用于权限请求结果
    public interface PermissionCallback {
        void onPermissionsGranted();

    }

    // 定义权限常量
    public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    public static final String PERMISSION_READ_MEDIA_IMAGES = Manifest.permission.READ_MEDIA_IMAGES; // API 33+
    public static final String PERMISSION_READ_MEDIA_VIDEO = Manifest.permission.READ_MEDIA_VIDEO; // API 33+
    public static final String PERMISSION_READ_MEDIA_AUDIO = Manifest.permission.READ_MEDIA_AUDIO; // API 33+
    public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE; // API 32 及以下
    public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE; // API 32 及以下
    public static final String PERMISSION_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;

    // 私有构造方法，确保单例
    public PermissionHelper() {
    }

    // 获取单例实例
    public static PermissionHelper getInstance() {
        return INSTANCE;
    }

    // 检查单个权限是否已授予
    private boolean hasPermission(AppCompatActivity activity, String permission) {
        return ContextCompat.checkSelfPermission(
                activity,
                permission
        ) == PackageManager.PERMISSION_GRANTED;
    }

    // 通用权限请求方法
    // 通用权限请求方法，使用外部传入的 launcher
    private void requestPermission(AppCompatActivity activity, ActivityResultLauncher<String[]> launcher, String[] permissions, PermissionCallback callback) {
        List<String> permissionsToRequest = new ArrayList<>();
        for (String permission : permissions) {
            if (!hasPermission(activity, permission)) {
                permissionsToRequest.add(permission);
            }
        }

        if (permissionsToRequest.isEmpty()) {
            LogHelper.i("所有权限已授予，直接调用回调");
            callback.onPermissionsGranted();
        } else {
            LogHelper.i("请求权限: " + String.join(", ", permissionsToRequest));
            launcher.launch(permissionsToRequest.toArray(new String[0]));
        }
    }

    // 1. 请求录音权限
    public void requestRecordAudioPermission(AppCompatActivity activity, ActivityResultLauncher<String[]> launcher, PermissionCallback callback) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { // API 33+
            requestPermission(activity,launcher,new String[]{PERMISSION_RECORD_AUDIO,PERMISSION_READ_MEDIA_IMAGES}, callback);
        } else {
            requestPermission(activity,launcher,new String[]{PERMISSION_RECORD_AUDIO,PERMISSION_READ_EXTERNAL_STORAGE, PERMISSION_WRITE_EXTERNAL_STORAGE }, callback);
        }


    }

    // 2. 请求相机权限
    public void requestCameraPermission(AppCompatActivity activity, ActivityResultLauncher<String[]> launcher, PermissionCallback callback) {
        LogHelper.i("请求相机权限");
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { // API 33+
            requestPermission(activity, launcher, new String[]{
                    PERMISSION_CAMERA,
                    PERMISSION_RECORD_AUDIO
            }, callback);
        } else {
            requestPermission(activity, launcher, new String[]{
                    PERMISSION_CAMERA,
                    PERMISSION_RECORD_AUDIO
            }, callback);
        }
    }

    // 3. 请求相册权限（读取媒体文件）
    public void requestMediaImagesPermission(AppCompatActivity activity, ActivityResultLauncher<String[]> launcher, PermissionCallback callback) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { // API 33+
            requestPermission(activity, launcher,new String[]{PERMISSION_READ_MEDIA_IMAGES}, callback);
        } else {
            requestPermission(activity, launcher,new String[]{PERMISSION_READ_EXTERNAL_STORAGE}, callback);
        }
    }

    // 4. 请求读写文件权限（存储权限）
    public void requestStoragePermission(AppCompatActivity activity, ActivityResultLauncher<String[]> launcher, PermissionCallback callback) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { // API 33+
            requestPermission(activity,launcher, new String[]{
                    PERMISSION_READ_MEDIA_IMAGES,
                    PERMISSION_READ_MEDIA_VIDEO,
                    PERMISSION_READ_MEDIA_AUDIO
            }, callback);
        } else {
            requestPermission(activity,launcher, new String[]{
                    PERMISSION_READ_EXTERNAL_STORAGE,
                    PERMISSION_WRITE_EXTERNAL_STORAGE
            }, callback);
        }
    }

    // 5. 请求定位权限
    public void requestLocationPermission(AppCompatActivity activity, ActivityResultLauncher<String[]> launcher, PermissionCallback callback) {
        requestPermission(activity, launcher,new String[]{PERMISSION_LOCATION}, callback);
    }

    // 6. 请求所有权限的总方法
    public void requestAllPermissions(AppCompatActivity activity, ActivityResultLauncher<String[]> launcher, PermissionCallback callback) {
        List<String> allPermissions = new ArrayList<>();
        allPermissions.add(PERMISSION_RECORD_AUDIO);
        allPermissions.add(PERMISSION_CAMERA);
        allPermissions.add(PERMISSION_LOCATION);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
            allPermissions.add(PERMISSION_READ_MEDIA_IMAGES);
            allPermissions.add(PERMISSION_READ_MEDIA_VIDEO);
            allPermissions.add(PERMISSION_READ_MEDIA_AUDIO);
        } else {
            allPermissions.add(PERMISSION_READ_EXTERNAL_STORAGE);
            allPermissions.add(PERMISSION_WRITE_EXTERNAL_STORAGE);
        }

        requestPermission(activity, launcher,allPermissions.toArray(new String[0]), callback);
    }

    // 检查是否需要显示权限说明
    public boolean shouldShowPermissionRationale(AppCompatActivity activity, String permission) {
        return activity.shouldShowRequestPermissionRationale(permission);
    }
}